package Socket;


import jdk.nashorn.internal.runtime.FindProperty;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Socket01 {
    public static void main(String[] args) {
        //定义图片路径
        String img = "https://tse1-mm.cn.bing.net/th/id/OIP-C.eSS4x40qTlliW4ftI7-RNAHaNK?pid=ImgDet&rs=1";
        //使用URL访问图片
        try {
            URL url = new URL(img);
            //打开链接
            URLConnection connection = url.openConnection();
            //获取图片输入流
            InputStream in = connection.getInputStream();
            //获取输入流，写入到对应地址
            FileOutputStream filoOut = new FileOutputStream("F:\\桌面\\java企业级开发\\TestSocket\\img.jpg");
            //开始写入
            int b = -1;
            b = in.read();
            while(b!=-1){ //表示文件没有读取到末尾
                filoOut.write(b);
                filoOut.flush();//刷新缓冲区
                b = in.read();
            }
            //关闭流
            System.out.println("图片下载完成");
            filoOut.close();
            in.close();



        } catch (Exception e) {
            e.printStackTrace();
        }


    }


}
